﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 28/05/2009
// Time: 0:50
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;

using ComponentFactory.Krypton.Toolkit;
using csharp.common;
using csharp.rad.finan.entries;

namespace csharp.rad.finan.researchs
{
	/// <summary>
	/// Description of frmResearchBpr.
	/// </summary>
	public partial class frmResearchBpr : KryptonForm
	{
		public csharp.common.CONSTANTS.BILL_TYPE bill_type;
		private csharp.common.GenerateSql _gsql = new csharp.common.GenerateSql();
		
		
		Bprs listBpr = new Bprs();
		
		//TODO: make a template of research and inherit this form		
		public frmResearchBpr()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			_gsql.commandType = csharp.common.GenerateSql.TCommandType.CMD_SELECT;
		}
		
		private void fillDataGridView()
		{
			Int32 tam;
			
    		dgvMain.Rows.Clear();
			tam = _gsql.getRowCount();
			for (int i=0; i < tam; i++)
			{
				dgvMain.Rows.Add(new string []{	_gsql.getRow(i)["bpr_id"].ToString(),
				                 				_gsql.getRow(i)["bpr_detail_id"].ToString(),
				                 				_gsql.getRow(i)["person_name"].ToString(),
				                 				_gsql.getRow(i)["bpr_historic"].ToString(),
				                 				Convert.ToDateTime(_gsql.getRow(i)["bpr_detail_expiration_date"]).ToString(common.CONSTANTS.dateFormatBrazilian),
				                 				_gsql.getRow(i)["status_name"].ToString()});
			}
		}
		
		void BtnEditClick(object sender, EventArgs e)
		{
			string bprId;
			if (dgvMain.SelectedRows.Count > 0)
			{
				bprId = dgvMain.CurrentRow.Cells["id"].Value.ToString();
				
				entries.frmRegisterBpr form = new csharp.rad.finan.entries.frmRegisterBpr(bprId);
				
				form.bill_type = bill_type;
				
				form.ShowDialog();
				
				BtnSearchClick(sender, e);
			}
		}
		
		void FrmResearchBprLoad(object sender, EventArgs e)
		{
			string firstDay = common.Util.primeiroDiaMes(DateTime.Now.Month, DateTime.Now.Year);
			
			dtpBeginDate.Value = Convert.ToDateTime(firstDay);
			filterRegisters();
			if(bill_type == CONSTANTS.BILL_TYPE.TOPAY)
			{			
				this.Text = "Contas a Pagar";
				lblPerson.Text = "Fornecedor";
			}
			else
			{
				this.Text = "Contas a Receber";
				lblPerson.Text = "Cliente";
			}
		}
		
		void BtnSearchClick(object sender, EventArgs e)
		{
			filterRegisters();
		}
		
		void filterRegisters()
		{
			String sql;
			if(bill_type == CONSTANTS.BILL_TYPE.TOPAY)
			{
				//listBpr.fillListToPay(dtpBeginDate.Text ,dtpEndDate.Text, txtSearch.Text);
				
				sql = "SELECT * FROM finan.v_bpr WHERE bpr_type = 'D' ";

			}
			else
			{
				sql = "SELECT * FROM finan.v_bpr WHERE bpr_type = 'C' ";

			}
			sql += "AND bpr_detail_expiration_date BETWEEN '"+dtpBeginDate.Value.ToString(common.CONSTANTS.dateFormatInternational)
				+"' AND  '"+dtpEndDate.Value.ToString(common.CONSTANTS.dateFormatInternational)
				+"' AND bpr_historic LIKE '%"+txtSearch.Text+"%' "
				+" AND person_name LIKE '%"+txtPerson.Text+"%' "
				+" ORDER BY bpr_detail_expiration_date";
				
			_gsql.sql = sql;
			if (!_gsql.executeSql())
				return;
			fillDataGridView();	
		}
		
		void BtnPayClick(object sender, EventArgs e)
		{
			if(dgvMain.SelectedRows.Count > 0)
			{
				frmBprDetail form = new frmBprDetail();
				String id = dgvMain.CurrentRow.Cells["bpr_detail_id"].Value.ToString();
				form.bprDetail.id = id;
				form.ShowDialog();
				
				filterRegisters();
			}
		}
		
		
		void BtnSairClick(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}
